﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Abp.Domain.Entities;
using MySqlX.XDevAPI.Relational;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities.Auditing;
using KelonProud.Administration.Dtos;
using KelonProud.Infrastructure;

namespace KelonProud.Administration.Domain.Authorization
{
    [Table("KpAttachment")]
    public class Attachment : Entity, ICreationAudited
    {
        //储存文件名称
        [MaxLength(80)]
        [Required]
        public virtual string FileName { get; set; }

        //缩略文件名称
        [MaxLength(80)]
        [Required]
        public virtual string ContractionsFileName { get; set; }

        //文件原始名称
        [MaxLength(200)]
        [Required]
        public virtual string DisplayName { get; set; }

        //文件路径
        [MaxLength(200)]
        public virtual string FileFolderPath { get; set; }

        public virtual DateTime CreationTime { get; set; }

        public virtual long? CreatorUserId { get; set; }

        [ForeignKey("CreatorUserId")]
        public virtual User User { get; set; }

        [MaxLength(128)]
        public virtual string ContentType { get; set; }

        [MaxLength(20)]
        [Required]
        public virtual string FileExtension { get; set; }

        public virtual int? SizeKb { get; set; }

        public bool IsImg => JuagFileIsImg();

        public bool IsDoc => JuagFileIsDoc();

        [NotMapped]
        public string ImageVirtualPath => FileFolderPath + FileName+ FileExtension;

        public string GetAccessoryTypeName()
        {
            if (FileExtension.Contains("xls"))
                return "xls";
            if (FileExtension.Contains("xlsx"))
                return "xls";
            if (FileExtension.Contains("docx"))
                return "doc";
            if (FileExtension.Contains("doc"))
                return "doc";
            if (FileExtension.Contains("ppt"))
                return "ppt";
            if (FileExtension.Contains("pptx"))
                return "ppt";
            if (FileExtension.Contains("zip"))
                return "zip";
            if (FileExtension.Contains("rar"))
                return "rar";
            if (FileExtension.Contains("7z"))
                return "7z";
            if (FileExtension.Contains("js"))
                return "js";
            if (FileExtension.Contains("css"))
                return "css";
            if (FileExtension.Contains("htm"))
                return "htm";
            if (FileExtension.Contains("pdf"))
                return "pdf";
            if (FileExtension.Contains("txt"))
                return "txt";
            if (FileExtension.Contains("sql"))
                return "txt";
            if (FileExtension.Contains("avi"))
                return "avi";
            if (FileExtension.Contains("jpg"))
                return "jpg";
            if (FileExtension.Contains("png"))
                return "png";
            if (FileExtension.Contains("gif"))
                return "gif";
            if (FileExtension.Contains("bmp"))
                return "bmp";
            if (FileExtension.Contains("tif"))
                return "tif";
            if (FileExtension.Contains("emf"))
                return "emf";
            if (FileExtension.Contains("wmf"))
                return "wmf";
            if (FileExtension.Contains("psd"))
                return "ps";

            if (FileExtension.Contains("dwg"))
                return "dwg";
            if (FileExtension.Contains("koc"))
                return "koc";
            if (FileExtension.Contains("mrs"))
                return "mrs";
            else
            {
                return "unkown";
            }
        }

        public AttachmentDto ToOutPut()
        {
            return new AttachmentDto
            {
                Id = Id,
                FileExtension = FileExtension,
                FileName = FileName,
                DisplayName = DisplayName,
                Size = SizeKb.HasValue ? Utility.CountSize(SizeKb.Value) : "0",
                ContentType = GetAccessoryTypeName(),
                UploadDateTime = CreationTime,
                IsImg = JuagFileIsImg(),
                IsDoc = JuagFileIsDoc()
            };
        }

        private bool JuagFileIsImg()
        {
            var fileExtensionList = new List<string>
            {
                ".png",
                ".jpg",
                ".bmp",
                ".ico",
                ".gif",
                ".jpeg",
                ".tiff"
            };

            return fileExtensionList.Contains(FileExtension);
        }

        private bool JuagFileIsDoc()
        {
            var fileExtensionList = new List<string>
            {
                ".txt",
                ".pdf",
                ".doc",
                ".docx",
                ".xls",
                ".xlsx",
                ".ppt",
                ".pptx"
            };

            return fileExtensionList.Contains(FileExtension);
        }
    }
}
